/***************************************************************************
 *                                                                         *
 * Copyright (c) 2007 - 2009 Nuvoton Technology Corp. All rights reserved.*
 *                                                                         *
 ***************************************************************************/
 

#ifdef ECOS
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "drv_api.h"
#include "diag.h"
#include "wbio.h"
#include "NUC900_GE.h"
#include "NUC900_reg.h"
#else
#include <stdio.h>
#include "NUC900_GE.h"
#include "wbio.h"
#ifdef _NUC900_
#include "NUC900_reg.h"
#else
#include "reg702.h"
#endif
#endif

typedef unsigned long	  uint32; // for make_color function
///////////////////////////////////////////////////////////////////////////////
//
// Function: make_color()
//
// Description:
//   Convert DIB color to DDB color. 
//
///////////////////////////////////////////////////////////////////////////////
uint32 make_color(int color)
{
  UINT32 r, g, b;
  
  if (GFX_BPP==8)
  {
    r = (color & 0x00e00000) >> 16; // 3 bits
    g = (color & 0x0000e000) >> 11; // 3 bits
    b = (color & 0x000000c0) >> 6;  // 2 bits
    return (r | g | b); 
  }
  else if (GFX_BPP==16)
  {
    r = (color & 0x00f80000) >> 8; // 5 bits
    g = (color & 0x0000fc00) >> 5; // 6 bits
    b = (color & 0x000000f8) >> 3; // 5 bits
    return (r | g | b);
  }
  else return (UINT32)color;
}

///////////////////////////////////////////////////////////////////////////////
//
// Function: clear_screen()
//
// Description:
//   Clear the on screen buffer with a specified color.
//   This function is a subset of solid_fill().
//
///////////////////////////////////////////////////////////////////////////////
void clear_screen(int color)
{
  UINT32 cmd32;
  UINT32 color32, dest_pitch, dest_dimension;
  
  #ifdef DEBUG
  printf("clear_screen()\n");
  #endif
  //printf("color=0x%x\n", color);
 
  color32 = make_color(color);
  //printf("color32= %lx\n", color32);
    
  cmd32 = 0xcc430040; 
  outpw(REG_2D_GEC, cmd32);
  outpw(REG_2D_GEBC, color32); // fill with background color
  
  dest_pitch = GFX_WIDTH << 16; // pitch in pixels
  outpw(REG_2D_GESDP, dest_pitch);
  
  outpw(REG_2D_GEDSXYL, 0); // starts from (0,0)
  
  dest_dimension = GFX_HEIGHT << 16 | GFX_WIDTH;
  outpw(REG_2D_GEDIXYL, dest_dimension);

  outpw(REG_2D_GETG, 1); 
  
  while ((inpw(REG_2D_GEINTS)&0x01)==0); // wait for command complete  

  outpw(REG_2D_GEINTS, 1); // clear interrupt status
}


///////////////////////////////////////////////////////////////////////////////
//
// Function: clear_screen_buffer()
//
// Description:
//   Clear the on-screen buffer to black by software. 
//
///////////////////////////////////////////////////////////////////////////////
void clear_screen_buffer(void)
{
  UINT8 *ptr8;
  int i;
  
  ptr8 = (UINT8 *)GFX_START_ADDR;
  for (i=0; i<GFX_SIZE; i++) *ptr8++ = 0x00; // clear buffer data
}


///////////////////////////////////////////////////////////////////////////////
//
// Function: set_write_mask()
//
// Description:
//   Set output data mask. 
//
///////////////////////////////////////////////////////////////////////////////
void set_write_mask(int mask)
{
  outpw(REG_2D_GEWPM, make_color(mask));
}

void set_source_orig_starting(void *ptr)
{
	outpw(REG_2D_GEXYSORG, (int)ptr);
}
void set_destination_orig_starting(void *ptr)
{
	outpw(REG_2D_GEXYDORG, (int)ptr);
}